<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>ADD USER</title>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" media="screen" th:href="@{/css/register.css}" />
</head>
<body>
<!-- particles.js container -->
<div id="particles-js" style="display: flex; align-items: center; justify-content: center;"></div>

<!-- 新增用户页面容器 -->
<div class="login-container">
    <div class="login-page">
        <div class="login-content">
            <div class="login-tit">ADD USER</div>
            <form th:action="@{/user/add}" method="post" id="add-form">
                <!-- 用户名 -->
                <div class="login-input">
                    <label>
                        <input type="text" placeholder="Username (3-15 characters)" name="username" required>
                        <small>Username must be between 3 and 15 characters.</small>
                    </label>
                </div>
                <!-- 密码 -->
                <div class="login-input">
                    <label>
                        <input type="password" placeholder="Password (6-15 characters)" name="pwd" required>
                        <small>Password must be between 6 and 15 characters.</small>
                    </label>
                </div>
                <!-- 邮箱 -->
                <div class="login-input">
                    <label>
                        <input type="email" placeholder="Email" name="email" required>
                    </label>
                </div>
                <!-- 手机号 -->
                <div class="login-input">
                    <label>
                        <input type="text" placeholder="Phone" name="phone">
                    </label>
                </div>
                <!-- QQ 号 -->
                <div class="login-input">
                    <label>
                        <input type="text" placeholder="QQ" name="qq">
                    </label>
                </div>
                <!-- 个人信息 -->
                <div class="login-input">
                    <label>
                        <textarea placeholder="Personal Info" name="info"></textarea>
                    </label>
                </div>
                <!-- 提交按钮 -->
                <div class="login-btn">
                    <div class="login-btn-left">
                        <span onclick="submitAdd()">ADD USER</span>
                    </div>
                </div>
                <!-- 返回用户列表页面 -->
                <div class="login-btn">
                    <a href="/users" class="btn-register">BACK TO USER LIST</a>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 错误弹窗 -->
<div id="error-modal">
    <div class="modal-content">
        <p>添加用户错误，请按照要求进行。</p>
        <button onclick="closeModal()">确定</button>
    </div>
</div>

<!-- 成功弹窗 -->
<div id="success-modal">
    <div class="modal-content">
        <p>用户添加成功！请返回用户列表。</p>
        <button onclick="redirectToUserList()">确定</button>
    </div>
</div>

<!-- scripts -->
<script th:src="@{/js/particles.js}"></script>
<script th:src="@{/js/app.js}"></script>
<script>
    function submitAdd() {
        // 获取表单元素
        const form = document.getElementById("add-form");
        const username = form.querySelector('input[name="username"]').value;
        const password = form.querySelector('input[name="pwd"]').value;

        // 验证用户名长度
        if (username.length < 3 || username.length > 15) {
            showModal();
            return;
        }

        // 验证密码长度
        if (password.length < 6 || password.length > 15) {
            showModal();
            return;
        }

        // 使用 Fetch API 发送 POST 请求
        fetch('/user/add', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: new URLSearchParams({
                username: username,
                pwd: password,
                email: form.querySelector('input[name="email"]').value,
                phone: form.querySelector('input[name="phone"]').value,
                qq: form.querySelector('input[name="qq"]').value,
                info: form.querySelector('textarea[name="info"]').value
            })
        })
            .then(response => response.text())
            .then(data => {
                if (data.includes("成功")) {
                    showSuccessModal();  // 显示成功弹窗
                } else {
                    showModal();  // 显示错误弹窗
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showModal();  // 显示错误弹窗
            });
    }

    // 显示错误弹窗
    function showModal() {
        document.getElementById("error-modal").style.display = "flex";
    }

    // 关闭错误弹窗
    function closeModal() {
        document.getElementById("error-modal").style.display = "none";
        // 清空表单
        document.getElementById("add-form").reset();
    }

    // 显示成功弹窗
    function showSuccessModal() {
        document.getElementById("success-modal").style.display = "flex";
    }

    // 关闭成功弹窗并重定向到用户列表页面
    function redirectToUserList() {
        document.getElementById("success-modal").style.display = "none";
        window.location.href = "/users";  // 重定向到用户列表页面
    }
</script>
</body>
</html>